package com.sq.subject.domain.handler.subject;

import com.sq.subject.common.enums.IsDeletedFlagEnum;
import com.sq.subject.common.enums.SubjectInfoTypeEnum;
import com.sq.subject.domain.bo.SubjectAnswerBO;
import com.sq.subject.domain.bo.SubjectInfoBO;
import com.sq.subject.infra.basic.entity.SubjectMultiple;
import com.sq.subject.infra.basic.entity.SubjectRadio;
import com.sq.subject.infra.basic.service.SubjectMultipleService;
import com.sq.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 多选策略处理器
 * @author quan
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler {
    
    @Resource
    private SubjectMultipleService subjectMultipleService;
    
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBo) {
        List<SubjectMultiple> subjectMultipleList = subjectInfoBo.getOptionList().stream().map(subjectAnswerBO -> {
            SubjectMultiple subjectMultiple = new SubjectMultiple();
            subjectMultiple.setSubjectId(subjectInfoBo.getSubjectId());
            subjectMultiple.setOptionType(subjectAnswerBO.getOptionType());
            subjectMultiple.setOptionContent(subjectAnswerBO.getOptionContent());
            subjectMultiple.setIsCorrect(subjectAnswerBO.getIsCorrect());
            subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            return subjectMultiple;
        }).toList();
        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectInfoBO query(SubjectInfoBO subjectInfoBO) {
        SubjectMultiple multiple = new SubjectMultiple();
        multiple.setSubjectId(subjectInfoBO.getId());
        List<SubjectMultiple> subjectMultipleList =  subjectMultipleService.queryByCondition(multiple);
        List<SubjectAnswerBO> subjectAnswerBOList = subjectMultipleList.stream().map(item -> {
            SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
            subjectAnswerBO.setOptionType(item.getOptionType());
            subjectAnswerBO.setOptionContent(item.getOptionContent());
            subjectAnswerBO.setIsCorrect(item.getIsCorrect());
            return subjectAnswerBO;
        }).toList();
        subjectInfoBO.setOptionList(subjectAnswerBOList);
        return subjectInfoBO;
    }
}
